package lt.liepasoft.locr.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import lt.liepasoft.locr.data.Symbol;

public class OcrImageRenderPanel extends JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = -3900431567288070071L;
	
	private Renderer 
		mMainImageRenderer, mSelectedSymbolRenderer;
	
	public OcrImageRenderPanel() {
		mMainImageRenderer = new EmptyRenderer();
		mSelectedSymbolRenderer = new EmptyRenderer();
	}
	
    @Override
    protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        mMainImageRenderer.paint(g2);
        mSelectedSymbolRenderer.paint(g2);        
    }

    public void setMainImage(BufferedImage mainImage) {
        if (mainImage != null){
        	mMainImageRenderer = new MainImageRenderer(mainImage);
            this.setPreferredSize(new Dimension(mainImage.getWidth(), mainImage.getHeight()));   
            this.updateUI();
            getParent().invalidate();
        } else {
        	mMainImageRenderer = new EmptyRenderer();
        }
    }

    public void setSelectedSymbol(Symbol symbol) {
    	if (symbol != null){
	    	mSelectedSymbolRenderer = new SelectedSymbolRenderer(symbol);
	        this.updateUI();
    	} else {
    		mSelectedSymbolRenderer = new EmptyRenderer();
    	}
    }                

    public void clear(){
    	mMainImageRenderer = new EmptyRenderer();
		mSelectedSymbolRenderer = new EmptyRenderer();
    }
    
    private interface Renderer {
    	public void paint(Graphics2D g2);
    }
    
    private class MainImageRenderer implements Renderer {
    	
    	private BufferedImage mMainImage;
    	
    	public MainImageRenderer(BufferedImage mainImage){
    		this.mMainImage = mainImage;
    	}
    	
    	@Override
    	public void paint(Graphics2D g2) {
    		g2.drawImage(mMainImage, 0, 0, null);                
            g2.drawRect(0, 0, mMainImage.getWidth(), mMainImage.getHeight());    		
    	}
    }
    
    private class SelectedSymbolRenderer implements Renderer {
    	private Symbol mSelectedSymbol;
    	private String character;
    	
    	public SelectedSymbolRenderer(Symbol symbol) {
    		this.mSelectedSymbol = symbol;
    		if (mSelectedSymbol.getCharacter() != null)
    			character=mSelectedSymbol.getCharacter().toString();
		}
    	
    	@Override
    	public void paint(Graphics2D g2) {
    		g2.setColor(Color.red);
            g2.drawRect(mSelectedSymbol.bounds.x1, mSelectedSymbol.bounds.y1, mSelectedSymbol.bounds.width, mSelectedSymbol.bounds.height);                        	
            g2.drawString(character, mSelectedSymbol.bounds.x1, mSelectedSymbol.bounds.y1-5);        
    	}
    }
    
    private class EmptyRenderer implements Renderer {
    	@Override
    	public void paint(Graphics2D g2) {}
    }
}    